﻿using DevExpress.Utils;
using DevTeam;

using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo.Aksesuar
{
    public partial class AksPOSipOlustur : XPopupForm
    {

        DataTable dtAksSip;
        public AksPOSipOlustur(DataTable dt)
        {
            InitializeComponent();
            dtAksSip = dt;
        }

        public DataTable DtAksPOSip
        {
            get { return dtSipLines; }
        }

        BindingSource bsAksPOSip, bsAksPOSipLines, bsAksPOLines;
        DataTable dtSipLines;
        private void AksPOSipOlustur_Load(object sender, EventArgs e)
        {
            TAM.AksPOSipTableAdapter = new Datasets.MyDataTableAdapters.AksPOSipTableAdapter();

            dtSipLines = dtAksSip.DefaultView.ToTable(false, "AksPOLinesId", "AksesuarId", "Notes", "QtyOrdered", "AnlasilanFiyat", "Termin", "SipNo", "VendorId");
            dtSipLines.Columns.Add("AksPOSipId", typeof(int));
            dtSipLines.Columns.Add("Tutar", typeof(int), "QtyOrdered*AnlasilanFiyat");

            DataTable dtAksPOSipLines = new DataTable();
            dtAksPOSipLines.Columns.Add("AksPOSipId", typeof(int));
            dtAksPOSipLines.Columns.Add("VendorId", typeof(int));
            dtAksPOSipLines.Columns.Add("AksesuarId", typeof(int));
            dtAksPOSipLines.Columns.Add("QtyOrdered", typeof(decimal));
            dtAksPOSipLines.Columns.Add("Tutar", typeof(decimal));

            ds.Tables.Add(dtSipLines);
            ds.Tables.Add(dtAksPOSipLines);

            DataRelation FK_AksPOLines_AksPOSip_AksPOSipId = new DataRelation("FK_AksPOLines_AksPOSip_AksPOSipId",
              ds.AksPOSip.AksPOSipIdColumn,
              dtSipLines.Columns["AksPOSipId"]);
            ds.Relations.Add(FK_AksPOLines_AksPOSip_AksPOSipId);

            DataRelation FK_AksPOSipLines_AksPOSip_AksPOSipId = new DataRelation("FK_AksPOSipLines_AksPOSip_AksPOSipId",
              ds.AksPOSip.AksPOSipIdColumn,
              dtAksPOSipLines.Columns["AksPOSipId"]);
            ds.Relations.Add(FK_AksPOSipLines_AksPOSip_AksPOSipId);

            bsAksPOSip = new BindingSource(ds, "AksPOSip");
            bsAksPOSipLines = new BindingSource(bsAksPOSip, "FK_AksPOSipLines_AksPOSip_AksPOSipId");
            bsAksPOLines = new BindingSource(bsAksPOSip, "FK_AksPOLines_AksPOSip_AksPOSipId");

            ds.AksPOSip.Columns.Add("ToplamAdet", typeof(decimal), "SUM(Child(FK_AksPOSipLines_AksPOSip_AksPOSipId).QtyOrdered)");
            ds.AksPOSip.Columns.Add("ToplamTutar", typeof(decimal), "SUM(Child(FK_AksPOSipLines_AksPOSip_AksPOSipId).Tutar)");
            //ds.AksPOSip.Columns.Add("SipNo", typeof(string));

            AddBsToList(bsAksPOSip, bsAksPOSipLines, bsAksPOLines);

            //var qrAksPOSip = from item in dtAksSip.AsEnumerable()
            //                    group item by new { VendorId = item.Field<int>("VendorId"), SipNo = item.Field<string>("SipNo") }
            //                         into grp
            //                         select new
            //                         {
            //                             VendorId = grp.Key.VendorId,
            //                             SipNo=grp.Key.SipNo,
            //                             Adet = grp.Count(),
            //                         };

            var qrAksPOSip = from item in dtAksSip.AsEnumerable()
                             group item by new { VendorId = item.Field<int>("VendorId") }
                                 into grp
                                 select new
                                 {
                                     VendorId = grp.Key.VendorId,
                                     Adet = grp.Count(),
                                 };

            var qrAksPOSipLines = from item in dtSipLines.AsEnumerable()
                                  group item by new { VendorId = item.Field<int>("VendorId"), AksesuarId = item.Field<int>("AksesuarId") }
                                      into grp
                                      select new
                                      {

                                          VendorId = grp.Key.VendorId,
                                          AksesuarId = grp.Key.AksesuarId,
                                          Toplam = grp.Sum(item => item.Field<decimal>("QtyOrdered")),
                                          ToplamTutar = grp.Sum(item => item.Field<decimal?>("Tutar"))
                                      };
            foreach (var poSipLines in qrAksPOSipLines)
            {
                DataRow drPOSipLines = dtAksPOSipLines.NewRow();

                drPOSipLines["VendorId"] = poSipLines.VendorId;
                drPOSipLines["AksesuarId"] = poSipLines.AksesuarId;
                drPOSipLines["QtyOrdered"] = poSipLines.Toplam;
                drPOSipLines["Tutar"] = poSipLines.ToplamTutar;
                dtAksPOSipLines.Rows.Add(drPOSipLines);
            }

            foreach (var poSip in qrAksPOSip)
            {
                DataRow drPOSip = ds.AksPOSip.NewAksPOSipRow();

                DataRow[] drSipNo = dtAksSip.Select("VendorId=" + poSip.VendorId);
                if (drSipNo.Length > 0)
                {
                    DataTable distSipno = drSipNo.CopyToDataTable().DefaultView.ToTable(true, "SipNo");
                    string SipNo = distSipno.GetValuesofSpecifiedColumn("SipNo", ",");
                    drPOSip["SipNo"] = SipNo;
                }

                drPOSip["VendorId"] = poSip.VendorId;
                drPOSip["SipVerenId"] = WhoAmI;
                ds.AksPOSip.Rows.Add(drPOSip);
                int aksPOSipId = drPOSip["AksPOSipId"].ToInt();

                foreach (DataRow poSipLines in dtSipLines.Select("VendorId=" + poSip.VendorId))
                {
                    poSipLines["AkspoSipId"] = aksPOSipId;
                }

                foreach (DataRow poSipLines in dtAksPOSipLines.Select("VendorId=" + poSip.VendorId))
                {
                    poSipLines["AkspoSipId"] = aksPOSipId;
                }
            }

            InitGridView(gvAksPOSip, bsAksPOSip);
            InitGridView(gvAksPOSipLines, bsAksPOSipLines);

            gvAksPOSip.MakeReadOnly("DvzId");

            gvAksPOSip.BestFitColumns();
            gvAksPOSipLines.BestFitColumns();

            gvAksPOSip.YanYanaGetir("AksPOSipId:SipNo");
            gvAksPOSipLines.MakeColumnInvisible("AksPOSipId", "VendorId");
            gvAksPOSip.MakeColumnInvisible("SiparisTarihi", "SipNo", "DvzId");
            gvAksPOSip.SetCaptions("SipNo:TalepNo", "VendorId:Tedarikci");

            gvAksPOSip.Columns["SipVerenId"].ColumnEdit = RI(Rp.V_User);

            gvAksPOSip.AddSummary("ToplamAdet", "ToplamTutar");
            gvAksPOSipLines.AddSummary("QtyOrdered", "Tutar");

            gvAksPOSip.FormatAsNumber(0, "ToplamAdet");
            gvAksPOSip.FormatAsNumber(1, "ToplamTutar");
            //gvAksPOSipLines.FormatAsNumber(3, "AnlasilanFiyat");
            gvAksPOSipLines.FormatAsNumber(1, "Tutar");
            gvAksPOSipLines.FormatAsNumber(0, "QtyOrdered");

            bbiOrder.ItemClick += bbiOrder_ItemClick;
            gvAksPOSip.RowCellStyle += gvAksPOSip_RowCellStyle;
            gvAksPOSipLines.RowCellStyle += gvAksPOSipLines_RowCellStyle;
        }

        void gvAksPOSipLines_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName == "QtyOrdered" ||
                e.Column.FieldName == "AksesuarId"
                )
                e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
        }

        void gvAksPOSip_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName == "VendorId"
                )
                e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
        }
        void bbiOrder_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvAksPOSip.EndEditing();
            foreach (DataRow dr in ds.AksPOSip)
            {
                dr["SiparisTarihi"] = DateTime.Now;
            }

            Save();

            //foreach (DataRow dr in ds.AksPOSip)
            //{

            //Mail.Send("kadirakis@gmail.com", "hotmail", "Yeni Sipariş", "");
            //Mail.SendEmailWithAttachmentCC("Aksesuar Satınalma", "kadirakis@gmail.com", "kadirakis@hotmail.com", "Yeni Sipariş."+talepNo, talepNo+" nolu siparişin dosyası ektedir", foyPdf);


            //string aksposipId = dr["AksPOSipId"].ToString();
            //DataTable dt = dtSipLines.Select("AksPOSipId=" + aksposipId).CopyToDataTable();

            //GridView gvTemp = new GridView();
            //GridControl gcTemp = new GridControl();

            //DevExpress.XtraPrinting.XlsxExportOptions o = new DevExpress.XtraPrinting.XlsxExportOptions();
            //o.TextExportMode = DevExpress.XtraPrinting.TextExportMode.Value;

            //gvTemp.BeginInit();
            //gcTemp.BeginInit();

            //gcTemp.MainView = gvTemp;
            //gvTemp.GridControl = gcTemp;
            //gvTemp.GridControl.DataSource = dt;


            //gvTemp.ExportToXlsx("D:\\report.xlsx", o);  
            //string path = Dialogs.SaveFile("Excel Document", "Excel Documents|*.xlsx");                
            //FileManager.RunFile(path);
            //}

            //MemoryStream ms=new MemoryStream();
            //gvAksPOSip.ExportToXlsx(ms);
            //byte[] xlsx = ms.ToArray();

            ////Mail.SendEmailWithAttachmentCC("Aksesuar Satınalma", "kadirakis@gmail.com", "kadirakis@hotmail.com", "Yeni Sipariş", "13-1484 nolu siparişin dosyası ektedir", xlsx);
            //Mail.Send("kadir.akis@karbel.com", "hotmail", "Yeni Sipariş","");

            this.DialogResult = DialogResult.OK;
        }
    }
}